﻿using System;
using System.Globalization;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC.Nozzle
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class NozzleCommand : HYNoTranExternalCommand
	{
		public override void FilterProcess()
		{
			base.FilterViewType.Add(1);
			base.FilterViewType.Add(2);
			base.FilterViewType.Add(4);
			base.FilterProcess();
		}

		public override Result HYExecute()
		{
			ISelectionFilter selectionFilter = new NozzleSelectionFilter();
			FormNozzleCalcCheck formNozzleCalcCheck = new FormNozzleCalcCheck();
			DialogResult dialogResult;
			do
			{
				dialogResult = formNozzleCalcCheck.ShowDialog();
				if (dialogResult == DialogResult.Abort)
				{
					try
					{
						Reference reference = base.Sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "请选择一个喷口");
						Element element = base.Doc.GetElement(reference);
						string strSe = "";
						string strD = "";
						if (element.GetParameter("射程(m)") != null && element.GetParameter("喉口直径") != null)
						{
							strSe = element.GetParameter("射程(m)").AsString();
							strD = UnitConvert.LengthToInternal(element.GetParameter("喉口直径").AsDouble()).ToString(CultureInfo.InvariantCulture);
						}
						formNozzleCalcCheck.RefreshTheUi(strSe, strD);
					}
					catch (Exception)
					{
					}
				}
			}
			while (dialogResult != DialogResult.OK && dialogResult != DialogResult.Cancel);
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
